package com.cyclingAssistant.project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AddressManage extends ListActivity {

	// static final String[] address_hardcode = new String[] { "Home", "Office",
	// "UBC" };
	//
	// @Override
	// public void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,
	// null, null, null);
	// startManagingCursor(cursor);
	//
	// // setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
	// // address_hardcode));
	// setListAdapter(new SimpleCursorAdapter(this,
	// android.R.layout.simple_expandable_list_item_1, cursor, new
	// String[]{People.NAME},new int[]{android.R.id.text1}));
	//
	// ListView lv = getListView();
	// lv.setTextFilterEnabled(true);
	//
	// lv.setOnItemClickListener(new OnItemClickListener() {
	// public void onItemClick(AdapterView<?> parent, View view,
	// int position, long id) {
	// // When clicked, show a toast with the TextView text
	// Toast.makeText(getApplicationContext(),
	// ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
	// }
	// });
	// }
	// }

	private List<Map<String, Object>> mData;
	private MyAdapter adapter; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mData = getData();
		adapter = new MyAdapter(this);
		setListAdapter(adapter);

		// ListView lv = getListView();
		// lv.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// // When clicked, show a toast with the TextView text
		// Log.i("Addressbook-click", "hello");
		// Toast.makeText(getApplicationContext(),
		// ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
		// }
		// });
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("item_name", "Home");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("item_name", "Work");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("item_name", "School");
		list.add(map);

		return list;
	}

	// ListView 中某项被选中后的逻辑

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
//		Log.i("Addressbook-click", (String)mData.get(position).get("item_name"));
		showDialog();
	}
	protected void showDialog()
	{
		LayoutInflater inflater = LayoutInflater.from(this.getParent());
		final View textEntry = inflater.inflate(R.layout.address_dialog, null);
		
		final AlertDialog.Builder builder = new Builder(this.getParent());
//		builder.setCancelable(false);
		builder.setTitle("Edit Address");
		builder.setView(textEntry);
		builder.setPositiveButton("Save", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});

		builder.setNegativeButton("cancel", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		builder.show();
	}

	/**
	 * listview中点击按键弹出对话框
	 */
	public void showInfo(final int position) {
		new AlertDialog.Builder(this.getParent()).setTitle("Warning")
				.setMessage("Are you sure to delete?")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mData.remove(position);
						adapter.notifyDataSetChanged();
						//getListView().invalidate();
					}
				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				}).show();

	}

	public final class ViewHolder {
		public TextView addressView;
		public ImageButton deleteButton;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if (convertView == null) {

				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.list_item, null);
				holder.addressView = (TextView) convertView
						.findViewById(R.id.item_name);
				holder.deleteButton = (ImageButton) convertView
						.findViewById(R.id.item_delete_button);
				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}

			holder.addressView.setText((String) mData.get(position).get(
					"item_name"));

			holder.deleteButton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					showInfo(position);
				}
			});

			return convertView;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

	}

}
